﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Xml;
using System.Xml.Serialization;
using Oxite.Extensions;
using ECube.Web.CMS.Extensions;
namespace ECube.Web.CMS.Models
{
    public class WidgetContainer  : Widget, IXmlSerializable
    {

        public WidgetContainer()
        {            
            Widgets = new List<Widget>();
            Family = "基础部件容器";
            Title = Family + "1";
            this.ViewName = "WidgetContainer";
        }

        public WidgetContainer(String id):this()
        {
            Id = id;
        }       
        public List<Widget> Widgets{get; set;}

       
        public override  void RenderContent(HtmlHelper htmlHelper, string viewName)
        {
            
            if (string.IsNullOrEmpty(viewName))
            {
                htmlHelper.Write(
                    String.Format(@"<div id=""{0}"" {1}>",
                    Id,
                    IsDesign ? @"class=""widget-place""" : ""
                    ));

                foreach (Widget w in Widgets)
                {
                    w.Render(htmlHelper, IsDesign, IsEdit);
                }
                htmlHelper.Write("</div>");
            }
            else
            {
                htmlHelper.RenderPartialFromSkin(viewName, this);
            }
        }
      

        #region IXmlSerializable 成员

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            Id = reader.GetAttribute("id") ?? "";
            //System.Diagnostics.Debug.WriteLine(Id);
            if (reader.IsEmptyElement || !reader.Read())
            {
                reader.Read();
                return;
            }

            while (reader.NodeType != XmlNodeType.EndElement)
            {
                string stype = reader.GetAttribute("type");

                reader.ReadStartElement("WidgetType");

                Type t = typeof(Widget);
                if (!String.IsNullOrEmpty(stype))
                {
                    t = Type.GetType(stype);
                }

                // reader.ReadStartElement(t.Name);
                XmlSerializer xs = new XmlSerializer(t);
                Widgets.Add(xs.Deserialize(reader) as Widget);
                //reader.ReadEndElement();
                reader.ReadEndElement();
                reader.MoveToContent();
            }

            reader.ReadEndElement();

        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            if (!String.IsNullOrEmpty(Id))
            {
                writer.WriteAttributeString("id", Id);
            }
            Widgets.ForEach(w =>
                {
                    if (!string.IsNullOrEmpty(w.type) && w.type != w.GetType().AssemblyQualifiedName)
                    {
                        Type t = Type.GetType(w.type, false, true);
                        Widget we = Activator.CreateInstance(t) as Widget;
                        if (we != null)
                        {
                            we.Id = w.Id;
                            we.Title = w.Title;
                            we.ViewName = w.ViewName;
                            we.Content = w.Content;
                            we.Family = w.Family;
                            w = we;
                        }

                    }

                    XmlSerializer xs = new XmlSerializer(w.GetType());
                    writer.WriteStartElement("WidgetType");
                    writer.WriteAttributeString("type", w.GetType().AssemblyQualifiedName);
                    xs.Serialize(writer, w);
                    writer.WriteEndElement();
                });
        }

        #endregion
    }
}
